home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / pvquan16.zip / ANIM / ANIMFLI.C next >
C/C++ Source or Header  |  1992-11-30  |  5KB  |  135 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1992, Frank van der Hulst             *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  ************************************************************************
  7.  *                                                                      *
  8.  * Authors:                                                             *
  9.  *        FvdH - Frank van der Hulst (Wellington, NZ)                   *
  10.  *                                                                      *
  11.  * Versions:                                                            *
  12.  *    V1.5 920401 FvdH - Released as part of PVQUAN15.ZIP               *
  13.  *    V1.6 921101 FvdH - Ported to OS/2                                 *
  14.  *                                                                      *
  15.  ************************************************************************/
  16.  
  17. /*
  18.   This program uses FLI.LIB to create FLIC (Autodesk Animator) files from a
  19.   series of 2D frames. */
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #ifdef __TURBOC__
  24. #include <dos.h>
  25. #include <mem.h>
  26. #include <io.h>
  27. #include <conio.h>
  28.  
  29. #endif
  30. #ifdef __GNUC__
  31. #define cdecl
  32.  
  33. int getw(FILE *fp)
  34. {
  35.    int x;
  36.  
  37.    x = getc(fp);
  38.    return (getc(fp) << 8) | x;
  39. }
  40. #endif
  41.  
  42. #include "aatypes.h"
  43. #include "aascreen.h"
  44. #include "aaerr.h"
  45. #include "aafli.h"
  46. #include "aaflisav.h"
  47.  
  48. UBYTE palette[256][3];
  49.  
  50. void err_exit(char *msg)
  51. {
  52.    printf("%s\n", msg);
  53.    exit(1);
  54. }
  55.  
  56. /* Read one image from a 2D file, and load it into the buffers whose addresses
  57.    have been passed to it. Returns TRUE if successful, FALSE if not */
  58.  
  59. int read_image(char *fname, unsigned char *buff, unsigned char *palette)
  60. {
  61.    FILE *fp;
  62.    int maxcol, maxrow, colours;
  63.  
  64.    printf("Reading image %s\n", fname);
  65.    if ((fp = fopen(fname, "rb")) == NULL) return FALSE;
  66.  
  67.    if (getc(fp) != '2') err_exit("\nIncorrect signature on file.\n");
  68.    if (getc(fp) != 'D') err_exit("\nIncorrect signature on file.\n");
  69.  
  70.    maxcol = getw(fp);     /* store columns */
  71.    maxrow = getw(fp);     /* store rows */
  72.    if (maxcol != 320 || maxrow != 200) err_exit("\nInvalid image size -- must be 320*200.\n");
  73.    colours = getc(fp);
  74.    if (colours == 0) colours = 256;
  75.    fread(palette, 3, colours, fp);
  76.    fread(buff, 1, maxrow * maxcol, fp);
  77.    fclose(fp);
  78.    return TRUE;
  79. }
  80.  
  81. /* first_scr contains a copy of the first screen in the sequence, so that
  82.    the last frame->first frame wrap can be maintained. This is a little
  83.    wasteful of memory, but avoids having to decompress the first frame again
  84.    at the end. That saves having to port the decompression code too. */
  85.  
  86. void make_fli(char *in, char *out)
  87. {
  88.    Vscreen *curr_scr, *prev_scr, *first_scr;
  89.    Fli_head outhead;
  90.    FILE *outfile;
  91.    int file_no;
  92.    char in_fname[256];
  93.    int file_ok;
  94.    int result;
  95.  
  96.    if ((prev_scr  = aa_alloc_mem_screen()) == NULL) err_exit(fli_error_message(AA_ERR_NOMEM));
  97.    if ((curr_scr  = aa_alloc_mem_screen()) == NULL) err_exit(fli_error_message(AA_ERR_NOMEM));
  98.    if ((first_scr = aa_alloc_mem_screen()) == NULL) err_exit(fli_error_message(AA_ERR_NOMEM));
  99.    if ((outfile = fli_create(out, &outhead, 5)) == NULL) err_exit("Couldn't create output file.");
  100.    file_no = 0;
  101.    sprintf(in_fname, "%s.0", in);
  102.    file_ok = read_image(in_fname, curr_scr->pmap, curr_scr->cmap);
  103.    aa_copy_screen(curr_scr, first_scr);
  104.    if ((result = fli_write_next(outfile, &outhead, curr_scr, curr_scr)) < AA_SUCCESS) err_exit(fli_error_message(result));
  105.    while (file_ok) {
  106.       aa_copy_screen(curr_scr, prev_scr);
  107.       file_no++;
  108.       sprintf(in_fname, "%s.%d", in, file_no);
  109.       if (!read_image(in_fname, curr_scr->pmap, curr_scr->cmap)) break;
  110.       if ((result = fli_write_next(outfile, &outhead, curr_scr, prev_scr)) < AA_SUCCESS) err_exit(fli_error_message(result));
  111.    }
  112.    if ((result = fli_end(outfile, &outhead, prev_scr, first_scr)) < AA_SUCCESS) err_exit(fli_error_message(result));
  113.    fclose(outfile);
  114.    aa_free_mem_screen(prev_scr);
  115.    aa_free_mem_screen(curr_scr);
  116.    aa_free_mem_screen(first_scr);
  117. }
  118.  
  119. void cdecl main(int argc, char *argv[])
  120. {
  121.    char out_fname[256];
  122.  
  123.    printf("ANIMFLI v1.6 -- Create a FLI file containing an animation sequence.\n");
  124.    printf("By F van der Hulst. Copyright 1992\n\n");
  125.    if (argc != 3) {
  126.       printf("Usage: %s frame output\n", argv[0]);
  127.       printf("       frame  is the name (without extension) of the input frame files\n");
  128.       printf("       output is the name (without extension) of the FLI output file\n\n");
  129.       exit(1);
  130.    }
  131.  
  132.    sprintf(out_fname, "%s.fli", argv[2]);
  133.    make_fli(argv[1], out_fname);
  134. }
  135.